package com.ruoyi.web.controller.dongfang;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.question.domain.DfFile;
import com.ruoyi.question.service.IDfFileService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 文件 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-09-09
 */
@Controller
@RequestMapping("/question/dfFile")
public class DfFileController extends BaseController
{
    private String prefix = "question/dfFile";
	
	@Autowired
	private IDfFileService dfFileService;

	@RequiresPermissions("question:dfFile:view")
	@GetMapping()
	public String dfFile()
	{
	    return prefix + "/dfFile";
	}
	
	/**
	 * 查询文件列表
	 */
	@RequiresPermissions("question:dfFile:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(DfFile dfFile)
	{
		startPage();
        List<DfFile> list = dfFileService.selectDfFileList(dfFile);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出文件列表
	 */
	@RequiresPermissions("question:dfFile:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DfFile dfFile)
    {
    	List<DfFile> list = dfFileService.selectDfFileList(dfFile);
        ExcelUtil<DfFile> util = new ExcelUtil<DfFile>(DfFile.class);
        return util.exportExcel(list, "dfFile");
    }
	
	/**
	 * 新增文件
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存文件
	 */
	@RequiresPermissions("question:dfFile:add")
	@Log(title = "文件", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(DfFile dfFile)
	{		
		return toAjax(dfFileService.insertDfFile(dfFile));
	}

	//新增文件
	@Log(title = "新增文件", businessType = BusinessType.INSERT)
	@PostMapping("/saveFile")
	@ResponseBody
	public AjaxResult saveFile(HttpServletRequest request) throws Exception {
		Long questionId = Long.parseLong(request.getParameter("questionId"));
		String files = request.getParameter("array");
		JSONObject.JSONArray jsonArray =  JSON.unmarshal(files,JSONObject.JSONArray.class);
		if (null!= jsonArray && jsonArray.size()>0){
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject =(JSONObject)jsonArray.get(i);
				DfFile dfFile = new DfFile();
				dfFile.setQuestionId(questionId);
				dfFile.setFileId(jsonObject.getStr("fileId"));
				dfFile.setFileName(jsonObject.getStr("fileName"));
				dfFile.setFileType(jsonObject.getStr("fileType"));
				dfFile.setFileSource(jsonObject.getStr("fileSource"));
				dfFile.setFileSource("1");
				dfFile.setCreatetime(new Date());
				dfFileService.insertDfFile(dfFile);
			}
		}
		return AjaxResult.success();
	}
	//修改文件
	@Log(title = "修改文件", businessType = BusinessType.UPDATE)
	@PostMapping("/editFile")
	@ResponseBody
	public AjaxResult editFile(HttpServletRequest request) throws Exception {
		Long questionId = Long.parseLong(request.getParameter("questionId"));
		String files = request.getParameter("array");
		JSONObject.JSONArray jsonArray =  JSON.unmarshal(files,JSONObject.JSONArray.class);
		if (null!= jsonArray && jsonArray.size()>0){
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jsonObject =(JSONObject)jsonArray.get(i);
				DfFile dfFile = new DfFile();
				dfFile.setQuestionId(questionId);
				dfFile.setFileId(jsonObject.getStr("fileId"));
				dfFile.setFileName(jsonObject.getStr("fileName"));
				dfFile.setFileType(jsonObject.getStr("fileType"));
				dfFile.setFileSource(jsonObject.getStr("fileSource"));
				dfFile.setFileSource("2");
				dfFile.setCreatetime(new Date());
				dfFileService.insertDfFile(dfFile);
			}
		}
		return AjaxResult.success();
	}
	//查询文件
	@Log(title = "文件", businessType = BusinessType.UPDATE)
	@PostMapping("/queryFileById")
	@ResponseBody
	public List<DfFile> queryFileById(HttpServletRequest request) throws Exception {
		Long questionId = Long.parseLong(request.getParameter("questionId"));
		DfFile dfFile=new DfFile();
		dfFile.setQuestionId(questionId);
		dfFile.setFileSource("1");
		return dfFileService.selectDfFileList(dfFile);
	}
	//查询以解答问题文件
	@Log(title = "文件", businessType = BusinessType.UPDATE)
	@PostMapping("/selectFileQuestion")
	@ResponseBody
	public List<DfFile> selectFileQuestion(HttpServletRequest request) throws Exception {
		Long questionId = Long.parseLong(request.getParameter("questionId"));
		DfFile dfFile=new DfFile();
		dfFile.setQuestionId(questionId);
//		return dfFileService.selectDfFileList(dfFile);
		return dfFileService.findFileQuestionChildById(dfFile);
	}

	/**
	 * 修改文件
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		DfFile dfFile = dfFileService.selectDfFileById(id);
		mmap.put("dfFile", dfFile);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存文件
	 */
	@RequiresPermissions("question:dfFile:edit")
	@Log(title = "文件", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(DfFile dfFile)
	{		
		return toAjax(dfFileService.updateDfFile(dfFile));
	}
	
	/**
	 * 删除文件
	 */
	@RequiresPermissions("question:dfFile:remove")
	@Log(title = "文件", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(dfFileService.deleteDfFileByIds(ids));
	}
	
}
